/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof.wget;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author Carlos
 */
public class HtmlParser
{

    private static String baseHost;
    private static ArrayList<String> result;

    public static List<String> extractLinks(String host, String filePath)
    {
        baseHost = host;
        result = new ArrayList();

        try
        {
            //Document doc = Jsoup.connect(url).get();
            String fileContent = getFileContents(filePath);
            Document doc = Jsoup.parse(fileContent);
            doc.setBaseUri("http://" + baseHost);

            Elements links = doc.select("a[href]");
            Elements media = doc.select("[src]");
            Elements imports = doc.select("link[href]");

            // href ...
            for (Element link : links)
            {
                addElementLinkToList(link);
            }

            // img ...
            for (Element src : media)
            {
                addElementSrcToList(src);
            }

            // js, css, ...
            for (Element link : imports)
            {
                //result.add(link.attr("abs:href"));
                addElementLinkToList(link);
            }
            
            clearDuplicates();
        }
        catch (Exception ex)
        {
            Logger.getLogger(ExecutorDownloader.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    /*public final static void main(String[] args) throws Exception
     {
     String site = "http://www.rgagnon.com/topics/java-language.html";
     List<String> links = HtmlParser.extractLinks(site);
     for (String link : links)
     {
     System.out.println(link);
     }
     }*/
    private static String getFileContents(String filePath)
    {
        //...checks on aFile are elided
        StringBuilder contents = new StringBuilder();

        try
        {
            //use buffering, reading one line at a time
            //FileReader always assumes default encoding is OK!
            BufferedReader input = new BufferedReader(new FileReader(filePath));
            try
            {
                String line = null;
                /*
                 * readLine is a bit quirky :
                 * it returns the content of a line MINUS the newline.
                 * it returns null only for the END of the stream.
                 * it returns an empty String if two newlines appear in a row.
                 */
                while ((line = input.readLine()) != null)
                {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            }
            finally
            {
                input.close();
            }
        }
        catch (IOException ex)
        {
            Logger.getLogger(ExecutorDownloader.class.getName()).log(Level.SEVERE, null, ex);
        }

        return contents.toString();
    }

    /**
     * Only adds elements from the same host
     *
     * @param Element e
     */
    private static void addElementLinkToList(Element e)
    {
        try
        {
            URL tmp = new URL(e.absUrl("href"));
            if (tmp.getHost().equals(HtmlParser.baseHost))
            {
                result.add(e.attr("abs:href"));
            }
        }
        catch (MalformedURLException ex)
        {
            Logger.getLogger(ExecutorDownloader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Only adds elements from the same host
     *
     * @param Element e
     */
    private static void addElementSrcToList(Element e)
    {
        try
        {
            URL tmp = new URL(e.absUrl("src"));
            if (tmp.getHost().equals(HtmlParser.baseHost))
            {
                result.add(e.attr("abs:href"));
            }
        }
        catch (MalformedURLException ex)
        {
            Logger.getLogger(ExecutorDownloader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void clearDuplicates()
    {
        final Set<String> setToReturn = new HashSet();
        final Set<String> set1 = new HashSet();

        for (String url : result)
        {
            setToReturn.add(url);
        }
        
        result.clear();
        result.addAll(setToReturn);
    }
}
